package com.aizuda.boot.modules.business.dict.service.impl;

import com.aizuda.boot.modules.business.dict.entity.CountryDictEntity;
import com.aizuda.boot.modules.business.dict.entity.vo.CountryDictRespDTO;
import com.aizuda.boot.modules.business.dict.mapper.CountryDictMapper;
import com.aizuda.boot.modules.business.dict.service.CountryDictService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 国家字典服务实现类
 */
@Slf4j
@Service
public class CountryDictServiceImpl extends ServiceImpl<CountryDictMapper, CountryDictEntity> implements CountryDictService {
    
    @Resource
    private CountryDictMapper countryDictMapper;
    
    @Override
    public List<CountryDictRespDTO> getAllCountries() {
        // 查询所有国家，按ID排序
        List<CountryDictEntity> entities = countryDictMapper.selectList(
                new LambdaQueryWrapper<CountryDictEntity>()
                        .orderByAsc(CountryDictEntity::getId)
        );
        
        // 转换为响应DTO
        return entities.stream()
                .map(entity -> {
                    CountryDictRespDTO dto = new CountryDictRespDTO();
                    BeanUtils.copyProperties(entity, dto);
                    return dto;
                })
                .collect(Collectors.toList());
    }
    
    @Override
    public String getCountryNameByCode(String countryCode) {
        if (countryCode == null) {
            return null;
        }
        
        CountryDictEntity entity = countryDictMapper.selectOne(
                new LambdaQueryWrapper<CountryDictEntity>()
                        .eq(CountryDictEntity::getCountryCode, countryCode)
                        .last("LIMIT 1")
        );
        
        return entity != null ? entity.getCountryName() : null;
    }
    
    @Override
    public Map<String, String> batchGetCountryNamesByCodes(List<String> countryCodes) {
        if (countryCodes == null || countryCodes.isEmpty()) {
            return Map.of();
        }
        
        // 过滤空值
        List<String> validCodes = countryCodes.stream()
                .filter(code -> code != null && !code.trim().isEmpty())
                .distinct()
                .collect(Collectors.toList());
        
        if (validCodes.isEmpty()) {
            return Map.of();
        }
        
        // 批量查询
        List<CountryDictEntity> entities = countryDictMapper.selectList(
                new LambdaQueryWrapper<CountryDictEntity>()
                        .in(CountryDictEntity::getCountryCode, validCodes)
        );
        
        // 转换为Map
        return entities.stream()
                .collect(Collectors.toMap(
                        CountryDictEntity::getCountryCode,
                        CountryDictEntity::getCountryName
                ));
    }
} 